﻿using PublisherManagementSystem.DAL;
using PublisherManagementSystem.Models;
using System;
using System.Collections.Generic;

namespace PublisherManagementSystem.Data
{
    public static class DataInitializer
    {
        public static void Initialize(PublisherDbContext context)
        {
            // 确保数据库存在
            context.Database.EnsureCreated();

            // 检查是否已有数据
            if (context.Provinces.Any())
            {
                return;   // 数据库已初始化
            }

            // 添加省份数据
            var provinces = new List<Province>
            {
                new Province { ProvinceName = "北京市" },
                new Province { ProvinceName = "上海市" },
                new Province { ProvinceName = "广东省" },
                new Province { ProvinceName = "江苏省" },
                new Province { ProvinceName = "浙江省" }
            };

            context.Provinces.AddRange(provinces);
            context.SaveChanges();

            // 添加城市数据
            var cities = new List<City>
            {
                new City { CityName = "北京市", ProvinceID = provinces[0].ProvinceID },
                new City { CityName = "上海市", ProvinceID = provinces[1].ProvinceID },
                new City { CityName = "广州市", ProvinceID = provinces[2].ProvinceID },
                new City { CityName = "深圳市", ProvinceID = provinces[2].ProvinceID },
                new City { CityName = "南京市", ProvinceID = provinces[3].ProvinceID },
                new City { CityName = "苏州市", ProvinceID = provinces[3].ProvinceID },
                new City { CityName = "杭州市", ProvinceID = provinces[4].ProvinceID },
                new City { CityName = "宁波市", ProvinceID = provinces[4].ProvinceID }
            };

            context.Cities.AddRange(cities);
            context.SaveChanges();

            // 添加区县数据
            var districts = new List<District>
            {
                new District { DistrictName = "东城区", CityID = cities[0].CityID },
                new District { DistrictName = "西城区", CityID = cities[0].CityID },
                new District { DistrictName = "朝阳区", CityID = cities[0].CityID },
                new District { DistrictName = "黄浦区", CityID = cities[1].CityID },
                new District { DistrictName = "徐汇区", CityID = cities[1].CityID },
                new District { DistrictName = "天河区", CityID = cities[2].CityID },
                new District { DistrictName = "福田区", CityID = cities[3].CityID },
                new District { DistrictName = "玄武区", CityID = cities[4].CityID },
                new District { DistrictName = "姑苏区", CityID = cities[5].CityID },
                new District { DistrictName = "西湖区", CityID = cities[6].CityID },
                new District { DistrictName = "鄞州区", CityID = cities[7].CityID }
            };

            context.Districts.AddRange(districts);
            context.SaveChanges();

            // 添加出版社数据
            var publishers = new List<Publisher>
            {
                new Publisher
                {
                    PublisherName = "人民教育出版社",
                    ProvinceID = provinces[0].ProvinceID,
                    CityID = cities[0].CityID,
                    DistrictID = districts[2].DistrictID,
                    Address = "海淀区中关村南大街17号院1号楼",
                    ContactPerson = "张三",
                    Phone = "010-12345678",
                    Email = "contact@pep.com.cn"
                },
                new Publisher
                {
                    PublisherName = "清华大学出版社",
                    ProvinceID = provinces[0].ProvinceID,
                    CityID = cities[0].CityID,
                    DistrictID = districts[2].DistrictID,
                    Address = "海淀区清华大学学研大厦A座",
                    ContactPerson = "李四",
                    Phone = "010-88888888",
                    Email = "contact@tup.tsinghua.edu.cn"
                }
            };

            context.Publishers.AddRange(publishers);
            context.SaveChanges();

            // 添加员工数据
            var staffs = new List<PublisherStaff>
            {
                new PublisherStaff
                {
                    PublisherID = publishers[0].PublisherID,
                    Name = "王五",
                    Gender = "男",
                    BirthDate = new DateTime(1985, 5, 15),
                    Phone = "13800138000",
                    Email = "wangwu@pep.com.cn",
                    Position = "编辑",
                    JoinDate = new DateTime(2010, 3, 1)
                },
                new PublisherStaff
                {
                    PublisherID = publishers[0].PublisherID,
                    Name = "赵六",
                    Gender = "女",
                    BirthDate = new DateTime(1990, 8, 20),
                    Phone = "13900139000",
                    Email = "zhaoliu@pep.com.cn",
                    Position = "策划",
                    JoinDate = new DateTime(2015, 7, 15)
                },
                new PublisherStaff
                {
                    PublisherID = publishers[1].PublisherID,
                    Name = "孙七",
                    Gender = "男",
                    BirthDate = new DateTime(1982, 10, 5),
                    Phone = "13700137000",
                    Email = "sunqi@tup.tsinghua.edu.cn",
                    Position = "总编",
                    JoinDate = new DateTime(2005, 2, 10)
                }
            };

            context.PublisherStaffs.AddRange(staffs);
            context.SaveChanges();
        }
    }
}